package com.example.controller;

import com.example.Result;
import com.example.annotation.AsyncMessageHandler;
import com.example.entity.DishComment;
import com.example.service.DishCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "菜品评价管理", description = "菜品评价相关接口")
@RestController
@RequestMapping("/dish-comment")
public class DishCommentController {

    @Autowired
    private DishCommentService dishCommentService;

    @Operation(summary = "获取菜品的评价列表")
    @GetMapping("/dish/{dishId}")
    public Result<List<DishComment>> getCommentsByDishId(
            @Parameter(description = "菜品ID") @PathVariable Long dishId) {
        List<DishComment> comments = dishCommentService.getCommentsByDishId(dishId);
        return Result.success(comments);
    }

    @Operation(summary = "获取学生的评价列表")
    @GetMapping("/student/{studentId}")
    public Result<List<DishComment>> getCommentsByStudentId(
            @Parameter(description = "学生ID") @PathVariable Long studentId) {
        List<DishComment> comments = dishCommentService.getCommentsByStudentId(studentId);
        return Result.success(comments);
    }

    @Operation(summary = "添加评价")
    @PostMapping
    @AsyncMessageHandler(queue = "comment.create.queue")
    public Result<Boolean> addComment(
            @Parameter(description = "评价信息") @RequestBody DishComment comment) {
        boolean success = dishCommentService.addComment(comment);
        return success ? Result.success(true) : Result.error("添加评价失败");
    }

    @Operation(summary = "更新评价")
    @PutMapping
    @AsyncMessageHandler(queue = "comment.update.queue")
    public Result<Boolean> updateComment(
            @Parameter(description = "评价信息") @RequestBody DishComment comment) {
        boolean success = dishCommentService.updateComment(comment);
        return success ? Result.success(true) : Result.error("更新评价失败");
    }

    @Operation(summary = "删除评价")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "comment.delete.queue")
    public Result<Boolean> deleteComment(
            @Parameter(description = "评价ID") @PathVariable Long id) {
        boolean success = dishCommentService.deleteComment(id);
        return success ? Result.success(true) : Result.error("删除评价失败");
    }

    @Operation(summary = "审核评价")
    @PutMapping("/review/{id}")
    public Result<Boolean> reviewComment(
            @Parameter(description = "评价ID") @PathVariable Long id,
            @Parameter(description = "审核状态：1-通过，2-拒绝") @RequestParam Integer status) {
        boolean success = dishCommentService.reviewComment(id, status);
        return success ? Result.success(true) : Result.error("审核评价失败");
    }

    @Operation(summary = "点赞评价")
    @PostMapping("/like/{id}")
    public Result<Boolean> likeComment(
            @Parameter(description = "评价ID") @PathVariable Long id) {
        boolean success = dishCommentService.likeComment(id);
        return success ? Result.success(true) : Result.error("点赞失败");
    }

    @Operation(summary = "获取菜品平均评分")
    @GetMapping("/rating/{dishId}")
    public Result<Double> getAverageRating(
            @Parameter(description = "菜品ID") @PathVariable Long dishId) {
        double rating = dishCommentService.getAverageRating(dishId);
        return Result.success(rating);
    }

    @PutMapping("/{id}/status")
    public boolean updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return dishCommentService.updateStatus(id, status);
    }
} 